package com.friends.library.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.friends.library.Dto.IssueBook;
import com.friends.library.persistence.HibernateUtil;

public class IssueBook1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 * @author HAVE A NICE DAY
	 */
	public IssueBook1() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int bookid = Integer.parseInt(request.getParameter("bookid"));
		String studentname = request.getParameter("studentname");
		String rollno = request.getParameter("rollno");
		String bookname = request.getParameter("bookname");
		String authorname = request.getParameter("authorname");
		String course = request.getParameter("course");
		String year = request.getParameter("year");
		String contactno = request.getParameter("contactno");
		try {

			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			IssueBook issueBook = new IssueBook();
			issueBook.setBookid(bookid);
			issueBook.setStudentname(studentname);
			issueBook.setRollno(rollno);
			issueBook.setBookname(bookname);
			issueBook.setAuthorname(authorname);
			issueBook.setCourse(course);
			issueBook.setYear(year);
			issueBook.setContactno(contactno);
			issueBook.setDateissued(new Date());

			// for updating the database book registration status

			int status = 0;
			Session session2 = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction2 = session2.beginTransaction();
			String sql = "update BookRegistration set status=:status where bookid=:bookid";
			Query query = session2.createQuery(sql);
			query.setInteger("bookid", bookid);
			query.setInteger("status", status);
			int result = query.executeUpdate();

			session.save(issueBook);
			transaction2.commit();
			transaction.commit();
			session2.close();
			session.close();
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<head>");
			out.println("</head>");
			out.println("<body>");
			out.println("<h1>");
			out.println("Book Issued Successfully");
			out.println("</h1>");
			out.println("<a href='issue.jsp'>Back</a>");
			out.println("</body>");
			out.println("</html>");

		}

		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
